import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.*;
import java.util.List;


public class eventAction {
    buttonAction buttonaction;
    mouseAction mouseaction;

    eventAction() {
        buttonaction = new buttonAction();
    }
    eventAction(Button[] button,Stage stage, Stage stage1)
    {
        buttonaction= new buttonAction(button,stage,stage1);
    }

    eventAction(Image image) {
        buttonaction = new buttonAction(image);
    }

    eventAction(ImageView imageView,Stage stage) {
        mouseaction = new mouseAction(imageView,stage);
    }

}

class mouseAction implements EventHandler<MouseEvent> {
    Stage winerstage;//获胜界面
    ImageView imageView;//空白拼图
    int row1,row2,col1, col2;//坐标

    mouseAction(ImageView imageView, Stage stage) {
        this.imageView = imageView;
        this.winerstage = stage;
    }
    @Override
    //点击图片的鼠标事件
    public void handle(MouseEvent event) {
        ImageView isChooseImageView = (ImageView) event.getSource();
        row1 = GridPane.getRowIndex(imageView);
        col1 = GridPane.getColumnIndex(imageView);
        row2 = GridPane.getRowIndex(isChooseImageView);
        col2 = GridPane.getColumnIndex(isChooseImageView);
        List<String> timeRangePlayer=null;
        List<String> stepRangePlayer=null;
        List<Integer> timeRange=null;
        List<Integer> stepRange=null;
        if (StartUI.range.set.get(6).compareTo("0") == 0) {
            timeRangePlayer = StartUI.range.NtimeRangePlayer;
            stepRangePlayer = StartUI.range.NstepRangePlayer;
            timeRange = StartUI.range.NtimeRange;
            stepRange = StartUI.range.NstepRange;
        } else {

            timeRangePlayer = StartUI.range.HtimeRangePlayer;
            stepRangePlayer = StartUI.range.HstepRangePlayer;
            timeRange = StartUI.range.HtimeRange;
            stepRange = StartUI.range.HstepRange;
        }
        if (row1 == row2) {
            if ((col1 == (col2 - 1))||(col1 == (col2 + 1))) {
                imageViewSwap(isChooseImageView);
                Constants.effect.seek(new Duration(1000));
                if (StartUI.range.set.get(5).compareTo("1") == 0) {
                    Constants.effect.play();
                }
                if (isFinish()) {
                    Constants.game.stop();
                    if (StartUI.range.set.get(4).compareTo("1") == 0) {
                        Constants.win.play();
                    }
                    TimeCount.ispause = true;
                    Constants.finalInsran = false;//进入榜单标志
                    Constants.finalIntran = false;
                    Constants.finalSran = 0;//名次标志
                    Constants.finalTran = 0;
                    //记录该玩家进入榜单的位置
                    for (int i = 0; i < stepRangePlayer.size(); i++) {
                        if (Constants.Step < stepRange.get(i)) {
                            Constants.finalInsran = true;
                            Constants.finalSran = i;
                            break;
                        }
                    }
                    for (int i = 0; i < timeRangePlayer.size(); i++) {
                        if (TimeCount.time < timeRange.get(i)) {
                            Constants.finalIntran = true;
                            Constants.finalTran = i;
                            break;
                        }
                    }
                    Constants.setWinerStage();
                    winerstage.show();
                }
            }
        }
        if (col1 == col2) {
            if ((row1 == (row2 - 1)) || (row1 == (row2 + 1))) {
                imageViewSwap(isChooseImageView);
                Constants.effect.seek(new Duration(1000));
                if (StartUI.range.set.get(5).compareTo("1") == 0) {
                    Constants.effect.play();
                }
                if (isFinish()) {
                    Constants.game.stop();
                    if (StartUI.range.set.get(4).compareTo("1") == 0) {
                        Constants.win.play();
                    }
                    TimeCount.ispause = true;
                    Constants.finalInsran = false;//进入榜单标志
                    Constants.finalIntran = false;
                    Constants.finalSran = 0;//名次标志
                    Constants.finalTran = 0;
                    //记录该玩家进入榜单的位置
                    for (int i = 0; i <stepRangePlayer.size(); i++) {
                        if (Constants.Step < stepRange.get(i)) {
                            Constants.finalInsran = true;
                            Constants.finalSran = i;
                            break;
                        }
                    }
                    for (int i = 0; i < timeRangePlayer.size(); i++) {
                        if (TimeCount.time < timeRange.get(i)) {
                            Constants.finalIntran = true;
                            Constants.finalTran = i;
                            break;
                        }
                    }
                    Constants.setWinerStage();
                    winerstage.show();
                }
            }
        }
    }
    //拼图的移动
    void imageViewSwap(ImageView imageView1) {
        GameUI.stepShow.setText(""+(++Constants.Step));
        GridPane.setRowIndex(imageView, row2);
        GridPane.setColumnIndex(imageView, col2);
        GridPane.setRowIndex(imageView1, row1);
        GridPane.setColumnIndex(imageView1, col1);
        int temp = Constants.position[row1][col1];
        Constants.position[row1][col1] = Constants.position[row2][col2];
        Constants.position[row2][col2] = temp;
    }
    //获胜的判断
    boolean isFinish() {
        for (int i = 0; i < StartUI.level; i++) {
            for (int j = 0; j < StartUI.level; j++) {
                if ((StartUI.level * j + i) != Constants.position[i][j]) {
                    if ((StartUI.level * j + i) == StartUI.level*StartUI.level-1) {
                        return true;
                    }
                    return false;
                }
            }
        }
        return true;
    }
}
class buttonAction implements EventHandler<ActionEvent>{
    Image image;//进行游戏的图片
    Button button[];
    Stage stage;//游戏界面
    Stage stage1;//显示图片界面
    Stage rangeStage;//排行榜界面

    buttonAction(){
    }
    buttonAction(Button[] button,Stage stage, Stage stage1) {
        this.button = button;
        this.stage=stage;
        this.stage1 = stage1;
    }
    buttonAction(Image image) {
        this.image = image;
    }

    @Override
    //转入游戏界面事件
    public void handle(ActionEvent event) {
        GameUI play = null;
        play = new GameUI(image);
        try {
            play.start(new Stage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //游戏按钮事件
    public void handle1(ActionEvent event) {
        for (int i = 0; i < button.length; i++) {
            if ((Button) event.getSource() == button[i]) {
                if (button[i].getText() == "暂停游戏") {
                    button[i].setText("继续游戏");
                    TimeCount.ispause = true;
                    return;
                }
                if (button[i].getText() == "查看图片") {
                    if (TimeCount.ispause == true) {
                        TimeCount.ispause = false;
                        new TimeCount().start();
                    }
                    stage.hide();
                    stage1.show();
                    return;
                }
                if (button[i].getText() == "继续" +
                        "游戏") {
                    button[i].setText("暂停游戏");
                    TimeCount.ispause = false;
                    new TimeCount().start();
                    return;
                }
                if (button[i].getText() == "返回游戏") {
                    stage1.close();
                    stage.show();
                    return;
                }
            }
        }
    }
    //重玩按钮事件
    public void handle2(ActionEvent event) throws Exception {
        TimeCount.ispause = true;
        TimeCount.time = 0;
        Constants.Step = 0;
        GameUI.timeShow.setText("" + Constants.Step);
        GameUI.stepShow.setText("" + TimeCount.time);
        stage.close();
        stage1.close();
        Constants.game.stop();
        new StartUI().start(new Stage());
    }
    //排行榜按钮事件
    public void handle3(ActionEvent event){
        rangeStage = new Stage();
        rangeStage.setResizable(false);
        List<String> timeRangePlayer=null;
        List<String> stepRangePlayer=null;
        List<Integer> timeRange=null;
        List<Integer> stepRange=null;
        if (StartUI.range.set.get(6).compareTo("0") == 0) {
            timeRangePlayer = StartUI.range.NtimeRangePlayer;
            stepRangePlayer = StartUI.range.NstepRangePlayer;
            timeRange = StartUI.range.NtimeRange;
            stepRange = StartUI.range.NstepRange;
            rangeStage.setTitle("普通排行榜");
        } else {

            timeRangePlayer = StartUI.range.HtimeRangePlayer;
            stepRangePlayer = StartUI.range.HstepRangePlayer;
            timeRange = StartUI.range.HtimeRange;
            stepRange = StartUI.range.HstepRange;
            rangeStage.setTitle("困难排行榜");
        }
        Label ti = new Label("              时间排行                                  步数排行");
        ti.setFont(Font.font("宋体", FontWeight.BOLD,17));
        Label label1 = new Label("玩家         用时          排名");
        label1.setFont(Font.font("宋体", FontWeight.BOLD,17));
        Label label2 = new Label("玩家         步数          排名");
        label2.setFont(Font.font("宋体", FontWeight.BOLD,17));
        HBox hBox = new HBox();
        hBox.setSpacing(45);
        hBox.getChildren().addAll(label1, label2);
        Label timep[] = new Label[timeRangePlayer.size()];
        Label stepp[] = new Label[stepRangePlayer.size()];
        Label time[] = new Label[timeRangePlayer.size()];
        Label step[] = new Label[stepRangePlayer.size()];
        Label ran1[] = new Label[timeRangePlayer.size()];
        Label ran2[] = new Label[stepRangePlayer.size()];
        for (int i = 0; i < timeRangePlayer.size(); i++) {
            timep[i] = new Label(timeRangePlayer.get(i));
            timep[i].setFont(Font.font("宋体", FontWeight.BOLD, 12));
            time[i] = new Label("" + timeRange.get(i)+"秒");
            time[i].setFont(Font.font("宋体", FontWeight.BOLD, 12));
            ran1[i] = new Label("" + (i + 1));
            ran1[i].setFont(Font.font("宋体", FontWeight.BOLD, 12));
        }
        for (int i = 0; i < stepRangePlayer.size(); i++) {
            stepp[i] = new Label(stepRangePlayer.get(i));
            stepp[i].setFont(Font.font("宋体", FontWeight.BOLD, 12));
            step[i] = new Label("" + stepRange.get(i)+"步");
            step[i].setFont(Font.font("宋体", FontWeight.BOLD, 12));
            ran2[i] = new Label("" + (i + 1));
            ran2[i].setFont(Font.font("宋体", FontWeight.BOLD, 12));
        }
        GridPane gridPane = new GridPane();
        for (int j = 0; j < timeRangePlayer.size(); j++) {
            gridPane.add(timep[j], 0, j);
            gridPane.add(time[j], 1, j);
            gridPane.add(ran1[j], 2, j);
            gridPane.add(stepp[j], 3, j);
            gridPane.add(step[j], 4, j);
            gridPane.add(ran2[j], 5, j);
        }
        Label bank[] = new Label[6];
        for (int i = 0; i < 6; i++) {
            bank[i] = new Label("         ");
            gridPane.add(bank[i], i, 6);
        }
        gridPane.setHgap(50);
        gridPane.setVgap(20);
        if (timeRangePlayer.size() == 0 && stepRangePlayer.size() == 0) {
            Label label = new Label("无人上榜");
            label.setFont(Font.font("宋体", FontWeight.BOLD, 34));
            HBox hBox1 = new HBox(label);
            hBox1.setPadding(new Insets(50));
            ImageView back = new ImageView(StartUI.background);
            back.setOpacity(0.5);
            StackPane stackPane = new StackPane(back, hBox1);
            Scene scene = new Scene(stackPane, 240, 170);
            rangeStage.setScene(scene);
        } else {
            VBox vBox = new VBox();
            vBox.getChildren().addAll(ti, hBox,gridPane);
            vBox.setSpacing(20);
            vBox.setPadding(new Insets(30,0,0,0));
            ImageView back = new ImageView(StartUI.background);
            back.setOpacity(0.5);
            StackPane stackPane = new StackPane(back, vBox);
            Scene scene = new Scene(stackPane,450,300);
            rangeStage.setScene(scene);
        }
        rangeStage.show();
    }
    //setting按钮事件
    public void handle4(ActionEvent event) {
        String temp = null;
        Stage settingStage = new Stage();
        Label musicl = new Label("音乐");
        musicl.setFont(Font.font("宋体", FontWeight.BOLD,17));
        if (StartUI.range.set.get(4).compareTo("0") == 0) {
            temp = StartUI.range.set.get(0);
        } else {
            temp = StartUI.range.set.get(1);
        }
        Button music = new Button(temp);
        music.setFont(Font.font("宋体", FontWeight.BOLD,17));
        music.setOnAction(event1 -> {
            if (music.getText()== StartUI.range.set.get(0)) {
                music.setText("开");
                StartUI.range.set.set(4, "1");
                StartUI.start.play();
                return;
            }
            if (music.getText() == StartUI.range.set.get(1)) {
                music.setText("关");
                StartUI.range.set.set(4, "0");
                StartUI.start.stop();
                return;
            }
            if (music.getText() == "开") {
                music.setText("关");
                StartUI.range.set.set(4, "0");
                StartUI.start.stop();
                return;
            }
            if (music.getText() == "关") {
                music.setText("开");
                StartUI.range.set.set(4, "1");
                StartUI.start.play();
                return;
            }
        });
        HBox hBox1 = new HBox(musicl, music);
        hBox1.setPadding(new Insets(10, 0, 10, 38));
        hBox1.setSpacing(50);
        Label effl = new Label("音效");
        effl.setFont(Font.font("宋体", FontWeight.BOLD,17));
        if (StartUI.range.set.get(5).compareTo("0") == 0) {
            temp = StartUI.range.set.get(0);
        } else {
            temp = StartUI.range.set.get(1);
        }
        Button eff = new Button(temp);
        eff.setFont(Font.font("宋体", FontWeight.BOLD,17));
        eff.setOnAction(event1 -> {
            if (eff.getText()== StartUI.range.set.get(0)) {
                eff.setText("开");
                StartUI.range.set.set(5, "1");
                return;
            }
            if (eff.getText() == StartUI.range.set.get(1)) {
                eff.setText("关");
                StartUI.range.set.set(5, "0");
                return;
            }
            if (eff.getText() == "开") {
                eff.setText("关");
                StartUI.range.set.set(5, "0");
                return;
            }
            if (eff.getText() == "关") {
                eff.setText("开");
                StartUI.range.set.set(5, "1");
                return;
            }
        });
        HBox hBox2 = new HBox(effl, eff);
        hBox2.setPadding(new Insets(10, 0, 10, 38));
        hBox2.setSpacing(50);
        Label hardl = new Label("难度");
        hardl.setFont(Font.font("宋体", FontWeight.BOLD,17));
        if (StartUI.range.set.get(6).compareTo("0") == 0) {
            temp = StartUI.range.set.get(2);
        } else {
            temp = StartUI.range.set.get(3);
        }
        Button hard = new Button(temp);
        hard.setFont(Font.font("宋体", FontWeight.BOLD,17));
        hard.setOnAction(event1 -> {
            if (hard.getText()== StartUI.range.set.get(2)) {
                hard.setText("困难");
                StartUI.range.set.set(6, "1");
                StartUI.level = 4;
                return;
            }
            if (hard.getText() == StartUI.range.set.get(3)) {
                hard.setText("普通");
                StartUI.range.set.set(6, "0");
                StartUI.level = 3;
                return;
            }
            if (hard.getText() == "普通") {
                hard.setText("困难");
                StartUI.range.set.set(6, "1");
                StartUI.level = 4;
                return;
            }
            if (hard.getText() == "困难") {
                hard.setText("普通");
                StartUI.range.set.set(6, "0");
                StartUI.level = 3;
                return;
            }
        });
        HBox hBox3 = new HBox(hardl, hard);
        hBox3.setPadding(new Insets(10, 0, 10, 35));
        hBox3.setSpacing(50);
        Button determine = new Button("确定");
        determine.setFont(Font.font("宋体", FontWeight.BOLD,17));
        determine.setOnAction(event1 -> {
            try (BufferedWriter setbr = new BufferedWriter(new FileWriter("set.txt"));){
                for (int i = 0; i < StartUI.range.set.size(); i++) {
                    setbr.write(StartUI.range.set.get(i));
                    setbr.newLine();
                }
                setbr.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            settingStage.close();
        });
        Button reset = new Button("重置");
        reset.setFont(Font.font("宋体", FontWeight.BOLD,17));
        reset.setOnAction(event1 -> {
            StartUI.range.set.set(4, "0");
            StartUI.range.set.set(5, "0");
            StartUI.range.set.set(6, "0");
            music.setText("关");
            eff.setText("关");
            hard.setText("普通");
            StartUI.level = 3;
        });
        HBox hBox4 = new HBox(determine, reset);
        hBox4.setPadding(new Insets(10, 0, 10, 20));
        hBox4.setSpacing(50);
        VBox vBox = new VBox(hBox1, hBox2, hBox3, hBox4);
        vBox.setPadding(new Insets(10));
        vBox.setSpacing(10);
        ImageView back = new ImageView(StartUI.background);
        back.setOpacity(0.5);
        StackPane stackPane = new StackPane(back, vBox);
        Scene scene = new Scene(stackPane, 220, 280);
        settingStage.setResizable(false);
        settingStage.setTitle("设置");
        settingStage.setScene(scene);
        settingStage.show();
    }
}